
package components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;


/**
 * 
 * set a massage to a player
 *
 */
public class MessageToPlayer extends JComponent  {

	private static final long serialVersionUID = 1L;
	
	private static final int CORNER = 40;	
	private static final int BOARD_END_WIDTH = 570;
	private static final int BOARD_END_HEIGHT = 650;
	private static final int RECT_WIDTH = 350;
	private static final int RECT_HEIGHT = 100;

	private static final Font DEFAULT_FONT = new Font ("Arial", Font.BOLD, 26);
	private static final Color DEFAULT_FONT_COLOUR = new Color (64, 128, 64);
	private static final Color BACKGROUND_COLOUR = new Color (255, 255, 255, 180);
	private static final Color BACKGROUND_STROKE_COLOUR = new Color (100, 100, 100);
	
	private Font font = null;
	private Color color = null;
	private String text = "";
	private FontMetrics fontMetrics = null;
	private RPSRoundRect roundRect = new RPSRoundRect(new Dimension(BOARD_END_WIDTH, BOARD_END_HEIGHT),
													  RECT_WIDTH, RECT_HEIGHT , CORNER ,
													  BACKGROUND_STROKE_COLOUR, BACKGROUND_COLOUR);

	
	public MessageToPlayer (String text) {
		this(text, DEFAULT_FONT_COLOUR, DEFAULT_FONT);

	}
	
	public MessageToPlayer (String text, Color color, Font font)
	{
		super();
		setOpaque(false);
		setBounds( (BOARD_END_WIDTH - RECT_WIDTH)/2 , (BOARD_END_HEIGHT - RECT_HEIGHT)/2 , RECT_WIDTH, RECT_HEIGHT);
		
		this.color = color;
		this.font = font;
		this.fontMetrics = getFontMetrics(font);
		this.text = text;
	}
	
	public void showMessage(){
		setVisible(true);
		repaint();
	}
	
	@Override
	public void paintComponent (Graphics g) {
		super.paintComponent (g);
		
		Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        roundRect.drawRect(g2);
        
        Rectangle2D rect = fontMetrics.getStringBounds(text, g2);
        
        if (getWidth() < rect.getWidth()) {
        	setSize((int)rect.getWidth() + 20, getHeight());
        }
        if (getHeight() < rect.getHeight()) {
        	setSize(getWidth(), (int)rect.getHeight() + 20);
        }
        
        g2.setFont(font);
    	g2.setColor (color);
		int tx = (int)(((getWidth() - rect.getWidth()) / 2));
		int ty = (int)(((getHeight() - rect.getHeight()) / 2)) + fontMetrics.getAscent();
		g2.drawString(text, tx, ty);
   }
}
